/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.xml;

import java.io.IOException;
import java.io.StringWriter;

import org.junit.Assert;
import org.junit.Test;

import pikes.xml.DocType;
import pikes.xml.XMLDocument;
import pikes.xml.XMLTag;
import pikes.xml.XMLVisitingException;
import pikes.xml.XMLVisitor;

public class XMLDocumentTestCase {

	@Test
	public void shouldFailWhenCreatingWithNullRootTag() throws Exception {
		try {
			new XMLDocument(null);
			Assert.fail();
		}
		catch (Exception e) {
			// left intentionally empty
		}

	}

	@Test
	public void testRoot() throws Exception {
		XMLDocument doc = new XMLDocument(new XMLTag("name"));
		StringWriter writer = new StringWriter();
		doc.print(writer);
		String actual = writer.toString();
		Assert.assertEquals("<?xml version=\"1.0\"?><name/>", actual);
	}

	@Test
	public void testRootAndDocType() throws Exception {

		XMLDocument doc = new XMLDocument(new XMLTag("name"), new DocType() {

			public void print(Appendable appendable) throws IOException {
				appendable.append("boo");
			}

			public void visit(XMLVisitor xmlProcessor) throws XMLVisitingException {
				xmlProcessor.publicDocType("boo", "id", "uri");

			}
		});
		StringWriter writer = new StringWriter();
		doc.print(writer);
		String actual = writer.toString();
		Assert.assertEquals("<?xml version=\"1.0\"?><!DOCTYPE boo PUBLIC \"id\" \"uri\"><name/>", actual);
	}

	@Test
	public void testProcess() throws Exception {
		XMLDocument doc = new XMLDocument(new XMLTag("name"));
		MockXMLProcessor xmlProcessor = new MockXMLProcessor();
		doc.visit(xmlProcessor);
		Assert.assertEquals("1.0", xmlProcessor.version);
		Assert.assertNull(xmlProcessor.encoding);
		Assert.assertEquals("name", xmlProcessor.firstName);
		Assert.assertEquals(Boolean.FALSE, xmlProcessor.hasChildren);
	}

	@Test
	public void testProcessWithencoding() throws Exception {
		XMLDocument doc = new XMLDocument(new XMLTag("name"), "UTF-8");
		MockXMLProcessor xmlProcessor = new MockXMLProcessor();
		doc.visit(xmlProcessor);
		Assert.assertEquals("1.0", xmlProcessor.version);
		Assert.assertEquals("UTF-8", xmlProcessor.encoding);
		Assert.assertEquals("name", xmlProcessor.firstName);
		Assert.assertEquals(Boolean.FALSE, xmlProcessor.hasChildren);
	}

	@Test
	public void testProcess1() throws Exception {
		MockDockType docType = new MockDockType();
		XMLDocument doc = new XMLDocument(new XMLTag("name"), docType);
		MockXMLProcessor xmlProcessor = new MockXMLProcessor();
		doc.visit(xmlProcessor);
		Assert.assertEquals("1.0", xmlProcessor.version);
		Assert.assertNull(xmlProcessor.encoding);
		Assert.assertEquals("name", xmlProcessor.firstName);
		Assert.assertEquals(Boolean.FALSE, xmlProcessor.hasChildren);
		Assert.assertSame(xmlProcessor, docType.xmlProcessor);
	}

	@Test
	public void testProcess1Withencoding() throws Exception {
		MockDockType docType = new MockDockType();
		XMLDocument doc = new XMLDocument(new XMLTag("name"), docType, "UTF-8");
		MockXMLProcessor xmlProcessor = new MockXMLProcessor();
		doc.visit(xmlProcessor);
		Assert.assertEquals("1.0", xmlProcessor.version);
		Assert.assertEquals("UTF-8", xmlProcessor.encoding);
		Assert.assertEquals("name", xmlProcessor.firstName);
		Assert.assertEquals(Boolean.FALSE, xmlProcessor.hasChildren);
		Assert.assertSame(xmlProcessor, docType.xmlProcessor);
	}
}
